/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function(head) {
  head = createLink(head);
  let slow = head;
  let fast = head.next;
  while(slow != fast) {
    if(!fast || !fast.next) {
      return false;
    }
    slow = slow.next;
    fast = fast.next.next;
  }
  return true;
};

/**
 * 构造链表
 * @param {*} arr 
 * @param {*} pos 
 */
function createLink(arr, pos) {
  let head = new ListNode(arr[0]);
  let listNodes = [head];
  arr.slice(1).reduce((prev, next) => {
    prev.next = new ListNode(next);
    listNodes.push(prev.next);
    return prev.next;
  }, head);
  if(pos != -1) {
    listNodes[listNodes.length - 1].next = listNodes[pos];
  }
  console.log(listNodes)
  return head;
}

console.log(hasCycle(createLink([1,2], 0)));

function ListNode(val) {
  this.val = val;
  this.next = null;
}